package com.hqq.rabbitmq.consumer;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;

/**
 * 基本类型消息的消费者
 * @author huangqiqin
 * @date 2021/04/14 00:37
 **/
@Component
public class BasicMessageConsumer {

    private static final Logger log = LoggerFactory.getLogger(BasicMessageConsumer.class);

    /**
     * 注入JSON序列化和反序化实例
     */
    @Autowired
    private ObjectMapper objectMapper;

    /**
     * 监听并接收消息队列中的消息，这里采用单一容器工厂实例
     * @param message
     */
    @RabbitListener(queues = "${mq.basic.queue.name}", containerFactory = "singleListenerContainer")
    public void consumeMessage(@Payload String message){
        try {
            // 将字节数组转成字符串
            //String message = new String(msg, "utf-8");
            System.out.println("==> 接收到RabbitMQ消息：" + message);
        } catch (Exception e){
            log.error("==> 接收RabbitMQ队列中消息异常", e);
        }
    }

}
